package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.platform.dto.AlarmLoggingDTO;
import com.iwomy.secureplat.platform.entity.*;
import com.iwomy.secureplat.platform.excel.AlarmLoggingExcel;
import com.iwomy.secureplat.platform.service.AlarmFaultLoggingService;
import com.iwomy.secureplat.platform.service.AlarmLoggingService;
import com.iwomy.secureplat.platform.service.BasicDeviceService;
import com.iwomy.secureplat.platform.vo.AlarmLoggingVO;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 报警记录表
 *
 * @author pig
 * @date 2023-11-21 15:08:54
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/alarmLogging")
@Tag(description = "alarmLogging", name = "报警记录表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AlarmLoggingController {

	private final AlarmLoggingService alarmLoggingService;

	private final BasicDeviceService basicDeviceService;

	private final AlarmFaultLoggingService faultLoggingService;

	/**
	 * 分页查询
	 *
	 * @param page            分页对象
	 * @param alarmLoggingDTO 报警记录表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_alarmLogging_view')")
	public R getAlarmLoggingPage(@ParameterObject Page page, @ParameterObject AlarmLoggingDTO alarmLoggingDTO) {
		return R.ok(alarmLoggingService.getAlarmLoggingPage(page, alarmLoggingDTO));
	}

	/**
	 * 设备详情页报警记录
	 *
	 * @param page            分页对象
	 * @param alarmLoggingDTO 报警记录表
	 * @return
	 */
	@Operation(summary = "设备详情页报警记录", description = "设备详情页报警记录")
	@GetMapping("/deviceDetailsPage")
	@PreAuthorize("@pms.hasPermission('platform_alarmLogging_view')")
	public R deviceDetailsPage(@ParameterObject Page page, @ParameterObject AlarmLoggingDTO alarmLoggingDTO) {
		return R.ok(alarmLoggingService.deviceDetailsPage(page, alarmLoggingDTO));
	}

	/**
	 * 分页查询
	 *
	 * @param page            分页对象
	 * @param alarmLoggingDTO imei报警记录表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/getImeiList")
	@PreAuthorize("@pms.hasPermission('platform_basicDeviceFaultRecord_view')")
	public R getImeiList(@ParameterObject Page page, @ParameterObject AlarmLoggingDTO alarmLoggingDTO) {
		return R.ok(alarmLoggingService.getImeiList(page, alarmLoggingDTO));
	}


	/**
	 * 通过id查询报警记录表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_alarmLogging_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(alarmLoggingService.getById(id));
	}

	/**
	 * 新增报警记录表
	 *
	 * @param alarmLogging 报警记录表
	 * @return R
	 */
	@Operation(summary = "新增报警记录表", description = "新增报警记录表")
	@SysLog("新增报警记录表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_alarmLogging_add')")
	public R save(@RequestBody AlarmLoggingEntity alarmLogging) {
		return R.ok(alarmLoggingService.save(alarmLogging));
	}

	/**
	 * 修改报警记录表
	 *
	 * @param alarmLoggingDTO 报警记录表
	 * @return R
	 */
	@Operation(summary = "修改报警记录表", description = "修改报警记录表")
	@SysLog("修改报警记录表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_alarmLogging_edit')")
	public R updateById(@RequestBody AlarmLoggingDTO alarmLoggingDTO) {

		AlarmLoggingEntity alarmLoggingEntity = BeanUtil.copyProperties(alarmLoggingDTO, AlarmLoggingEntity.class);

		return R.ok(alarmLoggingService.updateById(alarmLoggingEntity));
	}

	/**
	 * 处理报警记录
	 *
	 * @param alarmLoggingDTO 报警记录表
	 * @return R
	 */
	@Operation(summary = "处理报警记录", description = "处理报警记录")
	@SysLog("处理报警记录")
	@PutMapping("/confirmState")
	@PreAuthorize("@pms.hasPermission('platform_alarmLogging_confirmState')")
	public R confirmState(@RequestBody AlarmLoggingDTO alarmLoggingDTO) {
		AlarmLoggingEntity alarmLoggingEntity = new AlarmLoggingEntity();
		alarmLoggingEntity.setId(alarmLoggingDTO.getId());
		alarmLoggingEntity.setRemarks(alarmLoggingDTO.getRemarks());
		alarmLoggingEntity.setConfirmState(alarmLoggingDTO.getConfirmState());
		return R.ok(alarmLoggingService.updateById(alarmLoggingEntity));
	}


	/**
	 * 通过id删除报警记录表
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除报警记录表", description = "通过id删除报警记录表")
	@SysLog("通过id删除报警记录表")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_alarmLogging_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(alarmLoggingService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 处理报警记录
	 *
	 * @param alarmLoggingDTO 处理报警记录
	 * @return R
	 */
	@Operation(summary = "处理报警记录", description = "处理报警记录")
	@SysLog("处理报警记录")
	@PutMapping("handle")
	public R handle(@RequestBody AlarmLoggingDTO alarmLoggingDTO) {
		AlarmLoggingEntity alarmLoggingEntity = BeanUtil.copyProperties(alarmLoggingDTO, AlarmLoggingEntity.class);
		alarmLoggingEntity.setState(10);
		alarmLoggingEntity.setProcessingTime(LocalDateTime.now());
		alarmLoggingService.updateById(alarmLoggingEntity);
		BasicDeviceEntity basicDeviceEntity = new BasicDeviceEntity();
		AlarmLoggingVO alarmLogging = alarmLoggingService.getById(alarmLoggingDTO.getId());
		basicDeviceEntity.setId(alarmLogging.getDeviceId());

		if (Objects.equals(alarmLoggingDTO.getDeviceState(), 20)) {
			// 重置设备状态
			basicDeviceEntity.setDeviceState(20);
			basicDeviceService.updateById(basicDeviceEntity);
			return R.ok();
		}
		alarmLoggingEntity = alarmLoggingService.oneByImeiDesc(alarmLogging.getImei());
		AlarmFaultLoggingEntity alarmFaultLogging = faultLoggingService.oneByImeiDesc(alarmLogging.getImei());
		if (ObjectUtil.isNotNull(alarmLoggingEntity) && ObjectUtil.isNotNull(alarmFaultLogging)) {
			basicDeviceEntity.setDeviceState(alarmFaultLogging.getCreateTime().compareTo(alarmLoggingEntity.getCreateTime()) > 0 ? 30 : 40);
		} else if (ObjectUtil.isNotNull(alarmLoggingEntity)) {
			basicDeviceEntity.setDeviceState(40);
		} else if (ObjectUtil.isNotNull(alarmFaultLogging)) {
			basicDeviceEntity.setDeviceState(30);
		} else {
			basicDeviceEntity.setDeviceState(20);
		}

		basicDeviceService.updateById(basicDeviceEntity);
		return R.ok();
	}

	/**
	 * @Return R
	 */
	@Operation(summary = "近3天的报警统计", description = "近3天的报警统计")
	@GetMapping("/threeDaysAlarmCount")
	public R threeDaysAlarmCount() {
		return alarmLoggingService.threeDaysAlarmCount();
	}


	/**
	 * 批量处理报警记录
	 *
	 * @param alarmLoggingDTO id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除报警记录表", description = "通过id删除报警记录表")
	@SysLog("通过id删除报警记录表")
	@PutMapping("batchHandle")
	public R batchHandle(@RequestBody AlarmLoggingDTO alarmLoggingDTO) {
		for (Long id:alarmLoggingDTO.getIds()) {
			alarmLoggingDTO.setId(id);
			this.handle(alarmLoggingDTO);
		}


		return R.ok();
	}



	/**
	 * 导出excel 表格
	 * @param alarmLoggingDTO 导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	public List<AlarmLoggingExcel> export(@ParameterObject AlarmLoggingDTO alarmLoggingDTO) {
		List export = alarmLoggingService.export(alarmLoggingDTO);
		return BeanUtil.copyToList(export,AlarmLoggingExcel.class);
	}

}